package com.course.util;

import java.time.*;
import java.util.Date;

/**
 * Created by Justin on 2017/5/30.
 */
public class DateUtils {

    private static final ZoneId CST = ZoneId.of("Asia/Shanghai");

    public static LocalDateTime toLocalDateTime(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), CST);
    }

    public static LocalDate toLocalDate(Date date) {
        return toLocalDateTime(date).toLocalDate();
    }

    public static LocalTime toLocalTime(Date date) {
        return toLocalDateTime(date).toLocalTime();
    }

    public static Date toDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(CST).toInstant());
    }

    public static Date toDate(LocalDate localDate, LocalTime localTime) {
        return Date.from(localDate.atTime(localTime).atZone(CST).toInstant());
    }

    public static Date toDate(LocalDate localDate) {
        return Date.from(localDate.atTime(0, 0).atZone(CST).toInstant());
    }

    public static Date toDate(int year, int month, int day, int hour, int minute) {
        return Date.from(LocalDateTime.of(year, month, day, hour, minute).atZone(CST).toInstant());
    }

    public static int calcWeekIdx(LocalDate begin, LocalDate date) {
        Period between = Period.between(begin, date);
        int weekIdx = between.getDays() / 7;
        weekIdx = weekIdx > 0 ? weekIdx + 1 : 1;
        return weekIdx;
    }

    public static int calcWeekDay(LocalDate begin, LocalDate date) {
        Period between = Period.between(begin, date);
        int weekDay = between.getDays() - between.getDays() / 7 * 7;
        weekDay = weekDay > 0 ? weekDay + 1 : 1;
        return weekDay;
    }

    public static Date now() {
        return toDate(LocalDateTime.now(CST));
    }
}
